package com.my.network.socket;

import java.io.*;
import java.net.Socket;

/**
 * @author: marshal
 * @date: 2023-03-21 16:31
 */
public class MySocketClient {
    public static void main(String[] args) {
        try (
                Socket socket = new Socket("127.0.0.1", 8099);
                // 从socket中获取输入流
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                // 创建输出流
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        ) {
            // 向服务端写入消息
            String message = "我是客户端\n";
            bw.write(message);
            bw.flush();
            // 读取服务端的消息
            String s = br.readLine();
            System.out.println("读取来自服务端的消息：");
            System.out.println(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
